package light.head.controller.site;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.site.CommodityDAO;
import light.head.dao.site.CommodityRankDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.CommodityRank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 公司产品排行
 */
@Controller
@RequestMapping(Url.COMMODITYRANK)
public class CommodityRankController extends SupporterController{
	private CommodityRankDAO crDAO;
	private CommodityDAO commDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.COMMODITYRANK_INIT);
	}

	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.SORT, Parm.DELETED,Parm.OC);
		if (crDAO.findByMap(map, pb) == RESULT.SUCCESS){
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}		
		return new ModelAndView(Url.COMMODITYRANK_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(Integer code,Integer wgpc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (null != code) {
			CommodityRank cr = crDAO.get(code);
			request.setAttribute(Parm.COMMODITYRANK, cr);
		}
		return new ModelAndView(Url.COMMODITYRANK_DEIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(CommodityRank cr,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (cr.getCode() == null) {
			cr.setDeleted(false);
			cr.setCu(cu_code(request));
			cr.setOc(cu_organCode(request));
			cr.setCt(new Date());
			result = crDAO.add(cr);
		} else {
			result = crDAO.upd(cr);
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code!=null){
			CommodityRank cr = crDAO.get(code);
			cr.setDeleted(true);
			result=crDAO.upd(cr);
		}
		if(result==RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	//辅助查询
	@RequestMapping(Url.ADICOMMODITYINIT)
	public ModelAndView adiCommodityInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.ADICOMMODITY_INIT);
	}
	@RequestMapping(value = Url.ADICOMMODITYLIST)
	public ModelAndView adiCommodityList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.OC, Parm.DELETED);
		if (commDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.ADICOMMODITY_LIST);
	}
	
	@Autowired
	public void setCommDAO(CommodityDAO commDAO) {
		this.commDAO = commDAO;
	}
	@Autowired
	public void setCrDAO(CommodityRankDAO crDAO) {
		this.crDAO = crDAO;
	}
}
